package com.igroupes.ovineherd.model.attribute.date;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.dto.response.Meta;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.DateStyle;
import com.igroupes.ovineherd.model.Style.DateTimeStyle;
import com.igroupes.ovineherd.model.attribute.BasicAttribute;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.FormKey;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import com.igroupes.ovineherd.util.DateUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 日期时间
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class DateTimeBasicAttribute extends BasicAttribute {
    public static final String MIN_DATE_LABEL = "最小日期时间";
    public static final String MAX_DATE_LABEL = "最大日期时间";
    protected DateStyle minDate;
    protected DateStyle maxDate;

    public DateTimeBasicAttribute() {
        super();
        this.minDate = new DateTimeStyle(MIN_DATE_LABEL, "", false);
        this.maxDate = new DateTimeStyle(MAX_DATE_LABEL, "", false);
    }
    public Boolean verifyField(DateTimeBasicAttribute dateBasicAttribute) {
        super.verifyField(dateBasicAttribute);
        new DateTimeStyle().verify(dateBasicAttribute.getMinDate());
        new DateTimeStyle().verify(dateBasicAttribute.getMaxDate());
        return true;
    }

    public Boolean verifyFieldV2(DateTimeBasicAttribute basic) {
        super.verifyFieldV2(basic);
        new DateTimeStyle().verify(new DateTimeStyle(MAX_DATE_LABEL, basic.getMinDate().getValue(), false));
        new DateTimeStyle().verify(new DateTimeStyle(MIN_DATE_LABEL, basic.getMaxDate().getValue(), false));
        return true;
    }

    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        DateTimeBasicAttribute dateBasicAttribute = (DateTimeBasicAttribute) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        jsonObject.set("maxDate", Convert.toStr(dateBasicAttribute.getMaxDate().getValue()));
        jsonObject.set("minDate", Convert.toStr(dateBasicAttribute.getMinDate().getValue()));
        dataModelAttributeEntity.setJson(jsonObject);
        dataModelAttributeEntity.setDbNumber(1);
        fieldQuery.setLength(DateUtils.DATETIME_FORMAT.length());
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQueries.set(0,fieldQuery);
        return fieldQueries;
    }
    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        JSONObject jsonObject =  super.getAttribute(modelDataJsonAttributeQuery);
        jsonObject.set("minDate",new DateTimeStyle(MIN_DATE_LABEL, modelDataJsonAttributeQuery.getJson().get("minDate"), false));
        jsonObject.set("maxDate",new DateTimeStyle(MAX_DATE_LABEL, modelDataJsonAttributeQuery.getJson().get("maxDate"), false));
        return jsonObject;
    }

    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        super.verifyRequestData(modelAttributeEntity, value);
        List<FieldApiResponse> fieldApiResponses = new ArrayList<>();
        if(value == null){
            return fieldApiResponses;
        }
        JSONObject jsonObject = modelAttributeEntity.getJson();
        String minDate = jsonObject.get("minDate").toString();
        String maxDate = jsonObject.get("maxDate").toString();
        if(ObjectUtil.isNotNull(value)){

            if(!DateUtils.checkDate(value.toString(), DateUtils.DATETIME_FORMAT)){
                throw new ModelException(modelAttributeEntity.getName() + "传值格式不正确");
            }
            if(StringUtils.isNotBlank(minDate)){

                if(!DateUtils.timeVerification(minDate, value.toString(), DateUtils.DATETIME_FORMAT)){
                    throw new ModelException(modelAttributeEntity.getName()+"传值错误，时间不能小于"+ minDate);
                }
            }
            if(StringUtils.isNotBlank(maxDate)) {

                if(!DateUtils.timeVerification(value.toString(), maxDate, DateUtils.DATETIME_FORMAT)){
                    throw new ModelException(modelAttributeEntity.getName()+"传值错误，时间不能大于"+ maxDate);
                }
            }

            FieldApiResponse fieldApiResponse = new FieldApiResponse();
            fieldApiResponse.setDbName(modelAttributeEntity.getDbName());
            fieldApiResponse.setValue(value);
            fieldApiResponse.setIsStr(true);
            fieldApiResponse.setType(MySqlTypeKey.VARCHAR.getCode());
            fieldApiResponses.add(fieldApiResponse);
        }

        return fieldApiResponses;
    }

    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity) {
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        String minDate = jsonObject.get("minDate").toString();
        String maxDate = jsonObject.get("maxDate").toString();
        return new JSONObject(new DateTimeStyle(
                dataModelAttributeEntity.getName(),"",
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode())
                ,minDate, maxDate
        ), false);
    }
    @Override
    public Meta getMeta(ModelDataJsonAttributeQuery dataModelAttributeEntity){
        return new Meta(FormKey.DATETIME.getCode(), true, true, true);
    }
}
